<?php
namespace app\home\controller;


use app\admin\util\Response;
use app\home\model\EngineeringImgModel;
use app\home\model\EngineeringModel;
use app\home\model\EngineeringTypeModel;
use app\home\util\Page;
use app\home\validate\EngineeringValidate;
use think\Config;
use think\Request;

class Engineering extends BaseMessage
{
    public function __construct(Request $request = null)
    {
        $this->_template_add = 'add';
        $this->_template_detail = 'detail';
        $this->_template_index = 'index';
        $this->_template_ajax_list = 'index/engineering_list';
        $this->_model = new EngineeringModel();
        $this->_validate = new EngineeringValidate();
        $this->_img_model = new EngineeringImgModel();
        parent::__construct($request);
        $this->assign('engineering',1);
    }
    public function index()
    {
        $order = input('order','1','htmlspecialchars');
        $where = [];
        $where['status'] = 1;
        $where['engineering_type'] = $order;
        $list = $this->_model->where($where)->paginate(10);
        $this->assign('list',$list);
        $this->assign('order',$order);
        return view($this->_template_index);
    }

    protected function _before_add()
    {
        $type = new EngineeringTypeModel();
        $type_list = $type->select();
        $this->assign('type_list',$type_list);
        $this->assign('action',url('insert'));
        $id = input('id',0,'intval');
        $this->assign('back_url',url('index'));
        if($id != 0){
            $where = [];
            $where['id'] = $id;
            $where['uid'] = $this->_user_id;
            $vo = $this->_model->where($where)->find();
            if($vo){
                $this->assign('vo',$vo);
                $this->assign('action',url('update'));
            }
            $this->assign('back_url',url('user/index',['type'=>1]));
        }
    }

    public function insert(){
        $param = [];
        $param['engineering_type'] = input('engineering_type',0,'intval');
        $param['title'] = input('title','','htmlspecialchars');
        $param['contacts'] = input('contacts','','htmlspecialchars');
        $param['mobile'] = input('mobile','','htmlspecialchars');
        $param['detail'] = input('detail','','htmlspecialchars');
        $param['uid'] = $this->_user_id;
        if(Config::get('site.message_default_status') == 1){
            $param['status'] = 1;
        }else{
            $param['status'] = 0;
        }
        $imgs = input('img/a','','htmlspecialchars');
        $validate = new EngineeringValidate();
        $result = $validate->check($param);
        if($result === false){
            Response::json(-1,$validate->getError());
        }else{
            $model = new EngineeringModel([],['after_insert'=>function(EngineeringModel $model)use($imgs){
                $data = [];
                if(is_array($imgs)) {
                    $imgs = array_reverse($imgs);
                    foreach ($imgs as $value) {
                        $temp = [];
                        $temp['message_id'] = $model->getLastInsID();
                        $temp['path'] = $value;
                        $data[] = $temp;
                    }
                }
                $model->imgs()->saveAll($data);
            }]);
            $result = $model->isUpdate(false)->save($param);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }

    public function update(){
        $id = input('id',0,'intval');
        $where = [];
        $where['id'] = $id;
        $where['uid'] = $this->_user_id;
        if(!$this->_model->where($where)->find()){
            Response::json(-1,'您所更新的资料不存在');
        }
        $param = [];
        $param['engineering_type'] = input('engineering_type',0,'intval');
        $param['title'] = input('title','','htmlspecialchars');
        $param['contacts'] = input('contacts','','htmlspecialchars');
        $param['mobile'] = input('mobile','','htmlspecialchars');
        $param['detail'] = input('detail','','htmlspecialchars');
        $imgs = input('img/a','','htmlspecialchars');
        if(!is_array($imgs) || count($imgs) == 0){
            Response::json(-1,'至少上传1张图片');
        }
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $this->_model->save($param,$where);
            $this->_img_model->where(['message_id'=>$id])->delete();
            $data = [];
            foreach ($imgs as $value) {
                $temp = [];
                $temp['message_id'] = $id;
                $temp['path'] = $value;
                $data[] = $temp;
            }
            $this->_img_model->saveAll($data);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }

}
